package com.ruoyi.web.controller.manager;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.manager.DevNacos;
import com.ruoyi.system.domain.manager.NacosConfigInfo;
import com.ruoyi.system.domain.vo.NacosRequest;
import com.ruoyi.system.service.manager.IDevNacosService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * nacos服务器的配置Controller
 *
 * @author ruoyi
 * @date 2024-04-01
 */
@RestController
@RequestMapping("/manager/nacos")
public class DevNacosController extends BaseController {
    @Autowired
    private IDevNacosService devNacosService;

    /**
     * 查询nacos服务器的配置列表
     */
    @PreAuthorize("@ss.hasPermi('manager:nacos:list')")
    @GetMapping("/list")
    public TableDataInfo list(DevNacos devNacos) {
        startPage();
        List<DevNacos> list = devNacosService.selectDevNacosList(devNacos);
        return getDataTable(list);
    }

    /**
     * 查询nacos服务配置列表
     */
    @PreAuthorize("@ss.hasPermi('manager:nacos:list')")
    @GetMapping("/configInfo/list")
    public TableDataInfo list(NacosConfigInfo nacosConfigInfo) {
        startPage();
        List<NacosConfigInfo> list = devNacosService.selectNacosConfigInfoList(nacosConfigInfo);
        return getDataTable(list);
    }

    /**
     * 导出nacos服务器的配置列表
     */
    @PreAuthorize("@ss.hasPermi('manager:nacos:export')")
    @Log(title = "导出nacos服务器的配置列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DevNacos devNacos) {
        List<DevNacos> list = devNacosService.selectDevNacosList(devNacos);
        ExcelUtil<DevNacos> util = new ExcelUtil<DevNacos>(DevNacos.class);
        util.exportExcel(response, list, "nacos服务器的配置数据");
    }

    /**
     * 获取nacos服务器的配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('manager:nacos:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(devNacosService.selectDevNacosById(id));
    }

    /**
     * 新增nacos服务器的配置
     */
    @PreAuthorize("@ss.hasPermi('manager:nacos:add')")
    @Log(title = "新增nacos服务器的配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DevNacos devNacos) {
        return toAjax(devNacosService.insertDevNacos(devNacos));
    }

    /**
     * 修改nacos服务器的配置
     */
    @PreAuthorize("@ss.hasPermi('manager:nacos:edit')")
    @Log(title = "修改nacos服务器的配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DevNacos devNacos) {
        return toAjax(devNacosService.updateDevNacos(devNacos));
    }

    /**
     * 删除nacos服务器的配置
     */
    @PreAuthorize("@ss.hasPermi('manager:nacos:remove')")
    @Log(title = "删除nacos服务器的配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(devNacosService.deleteDevNacosByIds(ids));
    }

    @PostMapping("/updateNacosConfig")
    @PreAuthorize("@ss.hasPermi('manager:nacos:edit')")
    @Log(title = "修改nacos服务器的配置", businessType = BusinessType.UPDATE)
    public AjaxResult updateNacosConfig(@RequestBody NacosRequest request) {
        boolean success = devNacosService.updateNacosConfig(request);
        if (success) {
            return AjaxResult.success("Nacos配置更新成功");
        } else {
            return AjaxResult.error("Nacos配置更新失败");
        }
    }
}
